import 'dart:ui';

import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:yaml/yaml.dart';

const chineseLocale = Locale("zh", "CN");
const englishLocale = Locale("en", "US");


var currentLocale = Get.deviceLocale;
var fallbackLocale = englishLocale;


Map<String, String> _chineseLanguageMap = {};
Map<String, String> _englishLanguageMap = {};

Future<Map<String, String>> resolveLocaleAssets(String localeResourcePath) async {
  return await rootBundle.loadStructuredData<YamlMap>(localeResourcePath, (content) async => loadYaml(content))
  .then((YamlMap yamlMap) {
    Map<String, String> dataMap = {};
    yamlMap.forEach((key, value) {
      dataMap[key] = value.toString();
    });
    return dataMap;
  });
}

Future<void> init() async {
  _chineseLanguageMap = await resolveLocaleAssets("assets/locale/zh-cn.yml");
  _englishLanguageMap = await resolveLocaleAssets("assets/locale/en-us.yml");
}

void changeLanguage() {
  if (Get.locale == englishLocale) {
    Get.updateLocale(chineseLocale);
  } else if (Get.locale == chineseLocale) {
    Get.updateLocale(englishLocale);
  }
}

class Messages extends Translations {

  @override
  Map<String, Map<String, String>> get keys => {
    'zh_CN': _chineseLanguageMap,
    'en_US': _englishLanguageMap
  };
}